propsage.namespace("modules.admin.modules.projects");

modules.admin.modules.projects.attrSetting = (function($, ui) {
	var attrName = null;
	var modelName = null;
	var attrDefaultLabel = null;
	var clickObj = null;

	function initComponents() {
		$('#attr-setting-form').dialog({
			'width': 300,
			'height': 200,
			'autoOpen': false,
			'resizable': false,
			'buttons': {
				'Cancel': {
					'text': 'Cancel',
					'click': onDialogCancel
				},
				'OK': {
					'text': 'Update',
					'class': 'blue',
					'click': onSettingOK
				}
			},
			'open': onAttrSettingFormOpen
		});
	}


	function initEventHandlers() {
		$('td').on('click', 'a.setting', onPropertySettingClick);
	}

	function onDialogCancel(event) {
		attrName = null;
		modelName = null;
		attrDefaultLabel = null;
		$(this).dialog("close");
	}

	function onSettingOK(event) {
		var data = $('#project-attr-setting-form').serializeArray();
		var href = $('#project-attr-setting-form').attr('action');

		ui.doAjax('POST', href, data, onUpdateSettingSuccess, onUpdateSettingFailure);
	}

	function onUpdateSettingSuccess(response) {
		var data = null;
		var attribute = null;

		if (response.data) {
			data = response.data;
			attribute = data.attribute.replace('.', '-').toLowerCase();

			if (data.active === '1') {
				clickObj.removeClass('inactive');
				if ($('#' + attribute))
					$('#' + attribute).removeClass('inactive');
			} else {
				if (!clickObj.hasClass('inactive'))
					clickObj.addClass('inactive');
				if ($('#' + attribute))
					$('#' + attribute).addClass('inactive');
			}
		}
		$('#attr-setting-form').dialog("close");
	}

	function onUpdateSettingFailure(response) {
		$('#attr-setting-form').dialog("close");
	}

	function onAttrSettingFormOpen(event) {
		var href = baseUrl + '/admin/ajax/agency/getSetting/model_name/' + modelName;
		var data = {
			'attribute': attrName
		};
		var settings = null;
		var active = null;
		var default_label = null;
		var default_value = null;
		var label_changed = null;
		var default_value_changed = null;

//		$('#attr-setting-form').find("input[type=text], input[type=checkbox]").val("");

		ui.doAjax('POST', href, data, function(response) {
			settings = response.settings;

			active = (typeof settings.active === 'undefined') ? '1' : settings.active;
			default_label = settings.default_label;
			default_value = settings.default_value;
			label_changed = (typeof settings.label_changed === 'undefined') ? '0' : settings.label_changed;
			default_value_changed = (typeof settings.default_value_changed === 'undefined') ? '0' : settings.default_value_changed;

			$('#attr-setting-form').find('input[name="AgencySetting[setting][attribute]"]').val(attrName);
			$('#attr-setting-form').find('input[name="AgencySetting[setting][active]"]').prop('checked', (active === '1') ? true : false);
			$('#attr-setting-form').find('input[name="AgencySetting[setting][default_label]"]').val(default_label);
			$('#attr-setting-form').find('input[name="AgencySetting[setting][default_value]"]').val(default_value);
			$('#attr-setting-form').find('input[name="AgencySetting[setting][label_changed]"]').prop('checked', (label_changed === '1') ? true : false);
			$('#attr-setting-form').find('input[name="AgencySetting[setting][default_value_changed]"]').prop('checked', (default_value_changed === '1') ? true : false);
		});


		$('#attr-setting-form').dialog('option', 'title', attrDefaultLabel);

	}

	function onPropertySettingClick(event) {
		attrName = $(this).children().attr('alt');
		clickObj = $(this).parent();

		var temp = attrName.split('.');
		modelName = temp[0];
		attrDefaultLabel = $("td#" + temp[0].toLowerCase() + "-" + temp[1]).text();

		$('#attr-setting-form').dialog("open");
	}

	$(document).ready(function() {
		initComponents();
		initEventHandlers();
	});

}(jQuery, propsage.ui));